<!--
 * @Author: xiaxia
 * @Description: 
 * @Date: 2023-07-05 13:48:57
 * @LastEditTime: 2023-07-26 16:58:47
 * @FilePath: \pdf-print\src\App.vue
-->
<template>
  <div clas="app">
    <div>
      <el-button type="primary" @click="printPdf()">打印pdf</el-button>
    </div>
  </div>
</template>

<script>
  import { getFileBase64 } from './utils/printjs.js'
  import {getLodop} from './utils/LodopFuncs'
  import * as pdfjsLib from "pdfjs-dist"
  const pdfjsWorker = import("pdfjs-dist/build/pdf.worker.entry")
  pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker
  export default {
    name: 'App',
    data(){
      return{
      }
    },
    methods:{
      async printPdf() {
        let answer = null
        const url = "http://localhost:8000/CLodopDemos/PDFDemo.pdf";
        // const url = "http://localhost:8000/CLodopDemos/S823022809281.pdf";
        // const url = "http://localhost:8000/CLodopDemos/23932000000001066919.pdf";
        // const url = "http://localhost:8000/CLodopDemos/23932000000000623027(1).pdf";
        const zhUrl = await getFileBase64(url);
        let LODOP = getLodop();
        if (!LODOP) return;
        // LODOP.SET_LICENSES("ZCSB技术处研发中心", "545C44BD459DBBAFB79F18A0F8160A12", "", "");
        LODOP.PRINT_INIT("测试pdf打印");
        LODOP.SET_PRINT_PAGESIZE(0, 2200, 2970, "A4"); // 设置纸张大小
        
        // const loadingTask = pdfjsLib.getDocument(url);
        const loadingTask = pdfjsLib.getDocument(zhUrl);
        const pdf = await loadingTask.promise;
        const numPages = pdf.numPages;
        for (let i = 1; i <= numPages; i++) {
          let pdfId = "pdf" + i;
          const newCanvas = document.createElement("canvas");
          newCanvas.setAttribute("id", pdfId);
          const page = await pdf.getPage(i);
          const context = newCanvas.getContext("2d");
          const viewport = page.getViewport({ scale: 7 });
          newCanvas.height = viewport.height;
          newCanvas.width = viewport.width;
          console.log("视图", viewport, newCanvas.height, newCanvas.width);
          const renderContext = {
            canvasContext: context,
            viewport: viewport,
          };
          await page.render(renderContext).promise;
          console.log("Page rendered");
          const img = newCanvas.toDataURL("image/jpeg");
          console.log(111111);
          LODOP.NewPage();
          LODOP.ADD_PRINT_IMAGE(50, 0, "100%", "100%", img);
          LODOP.SET_PRINT_STYLEA(0,"Stretch",2);
          newCanvas.remove();
        }
        LODOP.SET_PRINT_COPIES(1) // 打印份数
        const printName = sessionStorage.getItem("printName"); // 获取默认打印机
        if (LODOP.SET_PRINTER_INDEX(printName)) {
          LODOP.PRINT(); // 静默打印
        } else {
          LODOP.PREVIEW(); // 预览
        }
        // LODOP.On_Return执行完成才结束函数
        return new Promise((resolve) => {
          LODOP.On_Return = function (TaskID, Value) {
            if(Value){
              answer = { success: true, msg: "发送打印请求成功" }
              console.log('发送打印请求成功');
            } else {
              answer = { success: false, msg: "发送打印请求失败" }
              console.log('发送打印请求失败');
            }
            resolve(answer); // 完成Promise
          }
        })
      },
    }
  }
</script>

<style>
.app {
  text-align: center;
}
canvas {
  border: 1px solid #ccc;
}
img {
  width: 600px;
  height: 500px;
  border: 1px solid red;
}
.content {
  display: flex;
  align-items: center;
  justify-content: center;
}
.pdf {
  margin-right: 20px;
}
</style>
